﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DAL.Service;
using DAL.Service.Impl;
using System.Collections;
using DomainModel;

namespace BIZ
{
    public class BorrowBooksRule
    {
        IBookService bookOper = new BookServiceImpl();
        IUserCardCostService usercardCost = new UserCardCostServiceImpl();
        ILibraryBookIndentService lbi = new LibraryBookIndentServiceImpl();
        IUserInfoService user = new UserInfoServiceImpl();
        /// <summary>
        /// 验证图书是否符合出借标准
        /// </summary>
        /// <param name="lb_id">图书id</param>
        /// <returns>图书不存在(0)、没有库存(1)、电子书不能进入订单流程(2)、可以出借(3)</returns>
        public int validationBooks(int lb_id)
        {
            IList li = bookOper.getLibraryBookbyid(lb_id);
            if (li.Count > 0)//图书是否存在
            {
                Hashtable hs = (Hashtable)li[0];
                if (hs["lb_ebook"].ToString() == "0")//电子书不能进入订单流程
                    return 2;
                else if (int.Parse(hs["lb_inventory"].ToString()) <= 0)//没有库存
                    return 1;
                else if (hs["lb_state"].ToString() != "1")//图书为下架或删除状态
                    return 4;
                else
                    return 3;
            }
            else
            {
                return 0;//图书不存在
            }
        }
        /// <summary>
        /// 判断该用户是否符合结束条件
        /// </summary>
        /// <param name="u_id">用户id</param>
        /// <param name="u_id">当前要借书本数</param>
        /// <returns></returns>
        public string validationUser(int u_id,int booksC)
        {
            //要加上判断会员是否被注销或是黑户
            IList userli = user.getuserinfobyid(u_id);
            if (userli.Count > 0)
            {
                Hashtable usertb = (Hashtable)userli[0];
                if (usertb["u_vip"].ToString() != "1")
                    return "erroruserrule";
            }
            else
            {
                return "erroruser";
            }


            string returnValue="";
            int cCount = 0;//当前已经用次数
            int Count = 0;//卡片有效次数
            int booksCount = 0;//当前手持书本量
            
            //判断是否有借书充值卡
            IList li = usercardCost.getUserTimes(u_id);
            if (li.Count > 0)
            {
                foreach(Hashtable tb in li)
                {
                    Count = int.Parse(tb["c_times"].ToString());
                    if (Count==0)//如果是vip半年卡或vip年卡，则不用判断剩余可借次数，只需要判断手持量
                    {
                        booksCount = lbi.getUserHaveBooks(u_id); //查询当前手持书本量
                        if ((4 - booksCount) >= booksC)//如果手持量小于四本
                        {
                            return "good," + tb["c_ucid"].ToString();
                        }
                        else
                        {
                            return "best4";//最多手持四本书
                        }
                    }
                    else//查询剩余次数并比较
                    {
                        cCount = lbi.getTimes(int.Parse(tb["c_ucid"].ToString()));//查询当前的已用次数
                        if (Count > cCount)//如果充值卡还有剩余次数
                        {
                            booksCount = lbi.getUserHaveBooks(u_id); //查询当前手持书本量
                            if ((4 - booksCount) >= booksC)//如果手持量小于四本
                            {
                                return "good," + tb["c_ucid"].ToString();
                            }
                            else
                            {
                                return "best4";//最多手持四本书
                            }
                        }
                        else
                        {
                            returnValue = "notimes";
                        }
                    }
                }
            }//
            else
            {
                return "nocard";//没有充值卡
            }
            return returnValue;
        }
    }
}
